<template>
  <div class="register">

    <div class="register-box" style="">
      <h3>注册新店店长</h3>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model.number="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>

        <el-button type="primary" @click="submitForm('ruleForm')" style="margin-right: 40px" plain>注&nbsp;&nbsp;&nbsp;&nbsp;册</el-button>
        <el-button @click="login" plain>去&nbsp;登&nbsp;录</el-button>

      </el-form>
    </div>
  </div>

</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Register",
  data() {
    var checkUserName = (rule, value, callback) => {
      console.log(value)
      if (!value) {
        return callback(new Error('用户名不能为空'));
      }
      callback();
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        if (this.ruleForm.checkPass !== '') {
          this.$refs.ruleForm.validateField('checkPass');
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.ruleForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: '',
        checkPass: '',
        username: ''
      },
      rules: {
        pass: [
          {validator: validatePass, trigger: 'blur'}
        ],
        checkPass: [
          {validator: validatePass2, trigger: 'blur'}
        ],
        userName: [
          {validator: checkUserName, trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let _this = this

          this.$axios.post("/emp/registerAdmin", this.ruleForm).then(
              (resp) => {
                if (resp.data.result) {
                  _this.$message.success(resp.data.message)
                  _this.$router.push("/login")
                } else {
                  _this.$message.error(resp.data.message)
                }
              }
          ).catch(
              function (error) {
                console.log(error)
              }
          )


          console.log(this.ruleForm)
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    login(){
      this.$router.push('/login')
    }
  }
}

</script>

<style scoped>
.register {
  margin-top: 0;
  height: 100vh;
  background-image: linear-gradient(to bottom right,#303133, #606266, #909399);
  padding-top: 200px;
}

.register-box {
  width: 30%;
  height: 330px;
  margin: 150px auto;
  padding-right: 50px;
  /*padding-top: 20px;*/
  border: #cccccc solid 1px;
  background-color: #ebeef5;
  border-radius: 10px;
  transition-duration: .8s;

}

.register-box:hover {
  transition-duration: .8s;
  box-shadow: 0px 0px 10px 7px rgba(255,255,255,0.3);
}
</style>
